<?php

namespace app\index\controller;

use think\Controller;
use think\Db;

class Reportquery extends Common
{
    //用户结算报表
    public function yhjsbb()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        if (!empty($data['name'])) {
            $name = trim($data['name']);
        } else {
            $name = '';
        }
        if (!empty($data['tel'])) {
            $tel = $data['tel'];
        } else {
            $tel = '';
        }
        if (!empty($data['lb_id'])) {
            $lb_id = $data['lb_id'];
        } else {
            $lb_id = 1;
        }
        if (!empty($data['name'])) {
            $where['y.xingming|y.bh|y.tel'] = ['like', "%" . $name . "%"];
        }
        if (!empty($data['tel'])) {
            $where['y.tel'] = ['like', "%" . $tel . "%"];
        }
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $where['yj.shijian'] = ['between time', [(int)$date_start, (int)$date_end]];
        $where['y.bz'] = '1';
        $where['y.sj_xulie'] = ['like', "%," . $user_id . ",%"];
        $info = db('yonghu_jiesuan')
            ->alias('yj')
            ->where($where)
            ->where('yj.hb_id', $lb_id)
            ->join('yonghu y', 'y.id=yj.yh_id')
            ->field('y.bh,y.sj_xulie,y.xingming,yj.qishi,yj.cun,yj.cun_num,yj.qu,yj.qu_num,yj.tiao,yj.jian_yongjin,yj.ping_yongjin,yj.geye_fee,yj.yingkui,yj.huabo,yj.zhuan,yj.churu_fee,yj.shijian')
            ->select();
        $info1 = db('yonghu_jiesuan_history')
            ->alias('yj')
            ->where($where)
            ->where('yj.hb_id', $lb_id)
            ->join('yonghu y', 'y.id=yj.yh_id')
            ->field('y.bh,y.sj_xulie,y.xingming,yj.qishi,yj.cun,yj.cun_num,yj.qu,yj.qu_num,yj.tiao,yj.jian_yongjin,yj.ping_yongjin,yj.geye_fee,yj.yingkui,yj.huabo,yj.zhuan,yj.churu_fee,yj.shijian')
            ->select();
        $info=array_merge_recursive($info,$info1);
        if (!empty($info)) {
            $data = [];
            foreach ($info as $k => $v) {
                if (in_array($v['bh'], $data)){
                    foreach ($data as $ok => $ov){
                        if ($ov == $v['bh']){
                            if ($info[$ok]['shijian'] > $v['shijian']) $info[$ok]['qishi'] = $v['qishi'];
                            $info[$ok]['cun'] += $v['cun'];
                            $info[$ok]['cun_num'] += $v['cun_num'];
                            $info[$ok]['tiao'] += $v['tiao'];
                            $info[$ok]['qu'] += $v['qu'];
                            $info[$ok]['qu_num'] += $v['qu_num'];
                            $info[$ok]['jian_yongjin'] += $v['jian_yongjin'];
                            $info[$ok]['ping_yongjin'] += $v['ping_yongjin'];
                            $info[$ok]['geye_fee'] += $v['geye_fee'];
                            $info[$ok]['yingkui'] += $v['yingkui'];
                            $info[$ok]['huabo'] += $v['huabo'];
                            $info[$ok]['zhuan'] += $v['zhuan'];
                            $info[$ok]['churu_fee'] += $v['churu_fee'];
                            unset($info[$k]);
                            break;
                        }else {
                            continue;
                        }
                    }
                }else {
                    $data[$k] = $v['bh'];
                    $info[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                }
            }
            $info = array_merge($info);
            foreach ($info as $k=>$v){
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $info[$k]['bh'])){
                            $info[$k]['bh'] = substr_replace( $info[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
            returnSuccess($info);
        } else {
            returnError('暂无数据');
        }
    }

    //代理结算报表
    public function dljsbb()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        if (!empty($data['name'])) {
            $name = trim($data['name']);
        } else {
            $name = '';
        }
        if (!empty($data['tel'])) {
            $tel = $data['tel'];
        } else {
            $tel = '';
        }
        if (!empty($data['lb_id'])) {
            $lb_id = $data['lb_id'];
        } else {
            $lb_id = 1;
        }
        if ($this->isCZY) {
            $user_id = $this->par_id;
            $jujianczy = $this->jujian;
            $par = db('jujian')->where('id', $jujianczy['par_id'])->find();
        } else {
            $user_id = $this->id;
        }
        if (!empty($data['name'])) {
            $where['j.xingming|j.bh|j.tel'] = ['like', "%" . $name . "%"];
        }
        if (!empty($data['tel'])) {
            $where['j.tel'] = ['like', "%" . $tel . "%"];
        }
        $where['ij.shijian'] = ['between time', [(int)$date_start, (int)$date_end]];
        $where['j.bz'] = '1';
        $or = "(j.sj_xulie like '%,$user_id,%' or j.id = $user_id)";
        $list = db('ib_jiesuan')
            ->alias('ij')
            ->join('jujian j', 'ij.yh_id =j.id', 'LEFT')
            ->join('toucun_zu tc', 'j.toucun_zu_id = tc.id')
            ->where($where)
            ->where($or)
            ->where('ij.hb_id', $lb_id)
            ->field('j.bh,j.sj_xulie,j.xingming,ij.cun,ij.ib_cun,ij.qu,ij.ib_qu,ij.tiao,ij.ib_tiao,ij.tc_fanyong,ij.fanyong,ij.geye_fee_fanyong,ij.rujin_chengben,ij.chujin_chengben,ij.qishi')
            ->select();
        $list1 = db('ib_jiesuan_history')
            ->alias('ij')
            ->join('jujian j', 'ij.yh_id =j.id', 'LEFT')
            ->join('toucun_zu tc', 'j.toucun_zu_id = tc.id')
            ->where($where)
            ->where($or)
            ->where('ij.hb_id', $lb_id)
            ->field('j.bh,j.sj_xulie,j.xingming,ij.cun,ij.ib_cun,ij.qu,ij.ib_qu,ij.tiao,ij.ib_tiao,ij.tc_fanyong,ij.fanyong,ij.geye_fee_fanyong,ij.rujin_chengben,ij.chujin_chengben,ij.qishi')
            ->select();
        $list=array_merge_recursive($list,$list1);
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                $list[$k]['zongshouyi'] = $v['fanyong'] - $v['rujin_chengben'] - $v['chujin_chengben'];
                if ($list[$k]['tc_fanyong'] > 0) {
                    $list[$k]['zongshouyi'] += $list[$k]['tc_fanyong'];
                } else {
                    $list[$k]['tc_fanyong'] = '不返';
                }
                if ($list[$k]['geye_fee_fanyong'] > 0) {
                    $list[$k]['zongshouyi'] += $list[$k]['geye_fee_fanyong'];
                }
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $list[$k]['bh'])){
                            $list[$k]['bh'] = substr_replace( $list[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
            returnSuccess($list);
        } else {
            returnError('暂无数据');
        }
    }

    //开仓单统计
    public function kcdtj()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        $where=[];
        if (!empty($data['name'])) {
            $where['jujian.xingming | jujian.bh | jujian.tel'] = ['like', "%".trim($data['name'])."%"];
        }
        $or = '';
        if ($this->isCZY) {
            $dl_yh_id = $this->par_id;
            $or = "y.sj_xulie like '%,$dl_yh_id,%'";
        } else {
            $dl_yh_id = $this->id;
            $or = "(y.sj_xulie like '%,$dl_yh_id,%' or j.id = '$dl_yh_id')";
        }
        //获取单个用户的合约数据
        $rows = db("(select * from order_jl where cj_num > 0 and kp_bz =1 and tuo_shijian >=$date_start and tuo_shijian <= $date_end union select * from order_jl_history where cj_num > 0 and kp_bz =1 and tuo_shijian >=$date_start and tuo_shijian <= $date_end)")
            ->alias('o')
            ->field('o.heyue_id,h.name as heyue_name,y.jujian_id,j.par_id,j.bh,j.xingming,SUM(o.cj_num) as cj_num,SUM(o.zong_cj_yongjin) as zong_cj_yongjin')
            ->join('yonghu y', 'o.yh_id = y.id')
            ->join('jujian j', 'y.jujian_id = j.id')
            ->join('heyue h', 'o.heyue_id = h.id')
            ->where($or)
//            ->where('y.bz', 1)
//            ->whereTime('o.tuo_shijian', 'between', [$date_start, $date_end])
//            ->where('o.cj_num', '>', 0)
//            ->where('o.kp_bz', 1)
            ->group('o.heyue_id,y.jujian_id')
//            ->fetchSql(true)
            ->select();
        /*******************************************下面的代码很扯淡*********************************************/
//        $rows1 = db("(select * from order_jl where cj_num > 0 and kp_bz =1 and tuo_shijian >=$date_start and tuo_shijian <= $date_end union select * from order_jl_history where cj_num > 0 and kp_bz =1 and tuo_shijian >=$date_start and tuo_shijian <= $date_end)")
//            ->alias('o')
//            ->field('o.heyue_id,h.name as heyue_name,y.id as jujian_id,y.par_id,y.bh,y.xingming,SUM(o.cj_num) as cj_num,SUM(o.zong_cj_yongjin) as zong_cj_yongjin')
//            ->join('jujian y', 'o.yh_id = y.id and o.lb =2')
//            ->join('heyue h', 'o.heyue_id = h.id')
//            ->join('jujian j', 'y.id = j.id')
//            ->where($or)
////            ->where('y.bz', 1)
////            ->whereTime('o.tuo_shijian', 'between', [$date_start, $date_end])
////            ->where('o.cj_num', '>', 0)
////            ->where('o.kp_bz', 1)
//            ->group('o.heyue_id,y.id')
////            ->fetchSql(true)
//            ->select();
//        $rows = array_merge_recursive($rows,$rows1);
        /*******************************************上面的代码很扯淡*********************************************/




        if (empty($rows)) {
            returnError('暂无数据');
        }
        //取出合约ID和合约NAME
        $heyue_arr = array_column($rows, 'heyue_name', 'heyue_id');
        //获取当前的用户
        $jujian_arr = Db::name('jujian')
            ->where("sj_xulie like '%,$dl_yh_id,%' or id ='$dl_yh_id'")
            ->where('bz', 1)
            ->where($where)
            ->where('lb_id', '<', 3)
            ->column('id,bh,xingming,sj_xulie');

        if (empty($jujian_arr)) {
            returnError('暂无数据');
            returnError('暂无数据');
        }
        $list = [];
        foreach ($jujian_arr as $k => $v) {
            $shouxufei = 0;
            foreach ($heyue_arr as $key => $value) {
                $list[$v['id']]['list'][$key] = 0;
            }
            foreach ($rows as &$row) {
                if ($v['id'] == $row['jujian_id']) {
                    $list[$v['id']]['list'][$row['heyue_id']] = $row['cj_num'];
                    $shouxufei += $row['zong_cj_yongjin'];
                }
            }
            $list[$v['id']]['shouxufei'] = $shouxufei;
            $list[$v['id']]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
            $list[$v['id']]['xingming'] = $v['xingming'];
            $list[$v['id']]['bh'] = $v['bh'];
            $list[$v['id']]['id'] = $v['id'];
        }
        foreach ($list as $kk => $vv) {
            $list[$kk]['list'] = array_values($vv['list']);
        }
        $result = array("rows" => array_values($list), "heyue" => array_values($heyue_arr));
        if (!empty($result)) {
            returnSuccess($result);
        } else {
            returnError('暂无数据');
        }
    }

    //平仓单统计
    public function pcdtj()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        $where=[];
        if (!empty($data['name'])) {
            $where['jujian.xingming | jujian.bh | jujian.tel'] = ['like', "%" .  trim($data['name']) . "%"];
        }
        $or = '';
        if ($this->isCZY) {
            $dl_yh_id = $this->par_id;
            $or = "y.sj_xulie like '%,$dl_yh_id,%'";
        } else {
            $dl_yh_id = $this->id;
            $or = "(y.sj_xulie like '%,$dl_yh_id,%' or j.id = '$dl_yh_id')";
        }
        //获取单个用户的合约数据
        $rows = db("(select * from order_jl where cj_num > 0 and kp_bz =2 and tuo_shijian >=$date_start and tuo_shijian <= $date_end union select * from order_jl_history where cj_num > 0 and kp_bz =2 and tuo_shijian >=$date_start and tuo_shijian <= $date_end)")
            ->alias('o')
            ->field('o.heyue_id,h.name as heyue_name,y.jujian_id,j.par_id,j.bh,j.xingming,SUM(o.cj_num) as cj_num,SUM(o.zong_cj_yongjin) as zong_cj_yongjin')
            ->join('yonghu y', 'o.yh_id = y.id')
            ->join('jujian j', 'y.jujian_id = j.id')
            ->join('heyue h', 'o.heyue_id = h.id')
//            ->where('y.bz', 1)
//            ->whereTime('o.tuo_shijian', 'between', [$date_start, $date_end])
//            ->where('o.cj_num', '>', 0)
//            ->where('o.kp_bz', 2)
            ->where($or)
            ->group('o.heyue_id,y.jujian_id')
            ->select();
        /*******************************************下面的代码很扯淡*********************************************/
//        $rows1 = db("(select * from order_jl where cj_num > 0 and kp_bz =2 and tuo_shijian >=$date_start and tuo_shijian <= $date_end union select * from order_jl_history where cj_num > 0 and kp_bz =2 and tuo_shijian >=$date_start and tuo_shijian <= $date_end)")
//            ->alias('o')
//            ->field('o.heyue_id,h.name as heyue_name,y.id as jujian_id,y.par_id,y.bh,y.xingming,SUM(o.cj_num) as cj_num,SUM(o.zong_cj_yongjin) as zong_cj_yongjin')
//            ->join('jujian y', 'o.yh_id = y.id and o.lb =2')
//            ->join('heyue h', 'o.heyue_id = h.id')
//            ->join('jujian j', 'y.id = j.id')
//            ->where($or)
////            ->where('y.bz', 1)
////            ->whereTime('o.tuo_shijian', 'between', [$date_start, $date_end])
////            ->where('o.cj_num', '>', 0)
////            ->where('o.kp_bz', 1)
//            ->group('o.heyue_id,y.id')
////            ->fetchSql(true)
//            ->select();
//        $rows = array_merge_recursive($rows,$rows1);
        /*******************************************上面的代码很扯淡*********************************************/










        if (empty($rows)) {
            returnError('暂无数据');
        }

        //取出合约ID和合约NAME
        $heyue_arr = array_column($rows, 'heyue_name', 'heyue_id');
        //获取当前的用户
        $jujian_arr = Db::name('jujian')
            ->where('sj_xulie', 'like', '%,' . $dl_yh_id . ',%')
            ->where('bz', 1)
            ->where($where)
            ->where('lb_id', '<', 3)
            ->column('id,bh,xingming,sj_xulie');
        if (empty($jujian_arr)) {
            returnError('暂无数据');
        }
        $list = [];
        foreach ($jujian_arr as $k => $v) {
            $shouxufei = 0;
            foreach ($heyue_arr as $key => $value) {
                $list[$v['id']]['list'][$key] = 0;
            }
            foreach ($rows as &$row) {
                if ($v['id'] == $row['jujian_id']) {
                    $list[$v['id']]['list'][$row['heyue_id']] = $row['cj_num'];
                    $shouxufei += $row['zong_cj_yongjin'];
                }
            }
            $list[$v['id']]['shouxufei'] = $shouxufei;
            $list[$v['id']]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
            $list[$v['id']]['xingming'] = $v['xingming'];
            $list[$v['id']]['bh'] = $v['bh'];
            $list[$v['id']]['id'] = $v['id'];
        }
        foreach ($list as $kk => $vv) {
            $list[$kk]['list'] = array_values($vv['list']);
        }

        $result = array("rows" => array_values($list), "heyue" => array_values($heyue_arr));
        if (!empty($result)) {
            returnSuccess($result);
        } else {
            returnError('暂无数据');
        }
    }

    //委托记录
    public function wtjl()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        if (!empty($data['xz_kaiping'])) {
            $xz_kaiping = $data['xz_kaiping'];
        } else {
            $xz_kaiping = '';
        }
        if (!empty($data['xz_heyue'])) {
            $xz_heyue = $data['xz_heyue'];
        } else {
            $xz_heyue = '';
        }
        if (!empty($data['xz_huobi'])) {
            $xz_huobi = $data['xz_huobi'];
        } else {
            $xz_huobi = '';
        }
        if (!empty($data['name'])) {
            $where['y.xingming | y.bh | y.tel'] = ['like', "%" .  trim($data['name']) . "%"];
        }

        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $where['oj.tuo_shijian'] = ['between time', [(int)$date_start, (int)$date_end]];
        $where['y.sj_xulie'] = ['like', "%," . $user_id . ",%"];
        if (!empty($xz_heyue)) {
            $where['h.id'] = $xz_heyue;
        }
        if (!empty($xz_kaiping)) {
            $where['oj.kp_bz'] = $xz_kaiping;
        }
        if (!empty($xz_huobi)) {
            $where['oj.hb_id'] = $xz_huobi;
        }
        $list = Db::name('order_jl')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.cj_shijian,oj.zhanghao_id')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
        $list1 = Db::name('order_jl_history')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.cj_shijian,oj.zhanghao_id')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
        $list = array_merge_recursive($list,$list1);


//        $lis = Db::name('order_jl')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.cj_shijian,oj.zhanghao_id')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $lis1 = Db::name('order_jl_history')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.cj_shijian,oj.zhanghao_id')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $list = array_merge_recursive($list,$lis,$lis1);









//        halt($list);
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if ($v['lb'] == '1') {
                    $list[$k]['lb'] = '买';
                } else {
                    $list[$k]['lb'] = '卖';
                }
                if ($v['kp_bz'] == '1') {
                    $list[$k]['kp_bz'] = '开仓';
                } else {
                    $list[$k]['kp_bz'] = '平仓';
                }
                if ($v['zhanghao_id'] > 0) {
                    $list[$k]['zhanghao_id'] = '弘苏组';
                } else {
                    $list[$k]['zhanghao_id'] = '盈透组';
                }
                switch ($v['hb_id']) {
                    case 1:
                        $list[$k]['hb_id'] = '人民币';
                        break;
                    case 2:
                        $list[$k]['hb_id'] = '港元';
                        break;
                    case 3:
                        $list[$k]['hb_id'] = '美元';
                        break;
                    case 4:
                        $list[$k]['hb_id'] = '欧元';
                        break;
                    default:
                        $list[$k]['hb_id'] = '未知货币';
                }
                if ($v['baodan'] == 0) {
                    $list[$k]['baodan'] = '市价成交';
                }
//                if($v['cj_shijian']!=0) {
//                    $list[$k]['cj_shijian'] = $list[$k]['cj_shijian'];
//                }else{
//                    $list[$k]['cj_shijian']='-';
//                }
//                if($v['tuo_shijian']!=0) {
//                    $list[$k]['tuo_shijian'] = $list[$k]['tuo_shijian'];
//                }else{
//                    $list[$k]['tuo_shijian']='-';
//                }
                $list[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $list[$k]['bh'])){
                            $list[$k]['bh'] = substr_replace( $list[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
        }
        if (!empty($list)) {
            returnSuccess($list);
        } else {
            returnError('暂无数据');
        }
    }

    //成交记录
    public function cjjl()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime('+23 hours 59 minute 59 seconds', strtotime($data['date_end']));
        } else {
            $date_end = strtotime(date('Y-m-d 23:59:59', time()));
        }
        if (!empty($data['name'])) {
            $where['y.xingming | y.bh | y.tel'] = ['like', "%" .  trim($data['name']) . "%"];
        }
//        if(!empty($data['currency'])){$currency=$data['currency'];}else{returnError('请选择要查询的币种');}
//        if($currency == '人民币'){
//            $currency = 1;
//        }
//        if($currency == '港币'){
//            $currency = 2;
//        }
//        if($currency == '美元'){
//            $currency = 3;
//        }
//        if($currency == '欧元'){
//            $currency = 4;
//        }
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $where['oj.tuo_shijian'] = ['between time', [(int)$date_start, (int)$date_end]];

        $where['y.sj_xulie'] = ['like', "%," . $user_id . ",%"];
        $where['oj.cj_num'] = ['<>', "0"];
//        $where['oj.hb_id']=$currency;
        $list = Db::name('order_jl')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.cj_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
        $list1 = Db::name('order_jl_history')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.cj_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
        $list = array_merge_recursive($list,$list1);

//        $lis = Db::name('order_jl')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.cj_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $lis1 = Db::name('order_jl_history')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.cj_price as baodan,oj.yongjin,oj.hb_id,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $list = array_merge_recursive($list,$lis,$lis1);



        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if ($v['lb'] == '1') {
                    $list[$k]['lb'] = '买';
                } else {
                    $list[$k]['lb'] = '卖';
                }
                if ($v['kp_bz'] == '1') {
                    $list[$k]['kp_bz'] = '开仓';
                } else {
                    $list[$k]['kp_bz'] = '平仓';
                }
                if ($v['zhanghao_id'] > 0) {
                    $list[$k]['zhanghao_id'] = '弘苏组';
                } else {
                    $list[$k]['zhanghao_id'] = '盈透组';
                }
                switch ($v['hb_id']) {
                    case 1:
                        $list[$k]['hb_id'] = '人民币';
                        break;
                    case 2:
                        $list[$k]['hb_id'] = '港元';
                        break;
                    case 3:
                        $list[$k]['hb_id'] = '美元';
                        break;
                    case 4:
                        $list[$k]['hb_id'] = '欧元';
                        break;
                    default:
                        $list[$k]['hb_id'] = '未知货币';
                }
                $list[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $list[$k]['bh'])){
                            $list[$k]['bh'] = substr_replace( $list[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
        }
        if (!empty($list)) {
            returnSuccess($list);
        } else {
            returnError('暂无数据');
        }
    }

    //平仓记录
    public function pcjl()
    {
        $data = input();
        if (!empty($data['date_start'])) {
            $date_start = strtotime($data['date_start']);
        } else {
            $date_start = strtotime(date('Y-m-d 00:00:00', time()));
        }
        if (!empty($data['date_end'])) {
            $date_end = strtotime($data['date_end']) + 24 * 3600;
        } else {
            $date_end = strtotime(date('Y-m-d', time()+ 24 * 3600));
        }
        if (!empty($data['name'])) {
            $name = trim($data['name']);
            $where['y.xingming|y.bh|y.tel'] = ['like', "%" . $name . "%"];
        }
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $where['oj.tuo_shijian'] = ['between time', [(int)$date_start, (int)$date_end]];
        $where['y.sj_xulie'] = ['like', "%," . $user_id . ",%"];
//        $where['oj.zong_cj_num']=['>',"0"];
        $where['oj.cj_num'] = ['>', "0"];
        $where['oj.kp_bz'] = 2;

        $list = Db::name('order_jl')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->join('heyue_zu zu', 'h.heyue_zu_id = zu.id')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,zu.heyue_cs,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.cj_jianping_yongjin as yongjin,oj.hb_id,oj.cj_shijian,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id,oj.jian_price,oj.cj_price')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
//        echo Db::name('order_jl')->getLastSql();die;
        $list1 = Db::name('order_jl_history')
            ->alias('oj')
            ->join('yonghu y', 'y.id=oj.yh_id', 'LEFT')
            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
            ->join('heyue_zu zu', 'h.heyue_zu_id = zu.id')
            ->where($where)
            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,zu.heyue_cs,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.cj_jianping_yongjin as yongjin,oj.hb_id,oj.cj_shijian,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id,oj.jian_price,oj.cj_price')
            ->order('oj.id desc,oj.tuo_shijian desc')
            ->select();
        $list = array_merge_recursive($list,$list1);

//        $lis = Db::name('order_jl')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->join('heyue_zu zu', 'h.heyue_zu_id = zu.id')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,zu.heyue_cs,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.cj_jianping_yongjin as yongjin,oj.hb_id,oj.cj_shijian,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id,oj.jian_price,oj.cj_price')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $lis1 = Db::name('order_jl_history')
//            ->alias('oj')
//            ->join('jujian y', 'y.id=oj.yh_id and oj.lb =2', 'LEFT')
//            ->join('baodan_zt bz', 'bz.id=oj.baodan_bz', 'LEFT')
//            ->join('kaiping_lb kl', 'kl.id=oj.kp_lb', 'LEFT')
//            ->join('heyue h', 'h.id=oj.heyue_id', 'LEFT')
//            ->join('heyue_zu zu', 'h.heyue_zu_id = zu.id')
//            ->where($where)
//            ->field('y.sj_xulie,y.bh,y.xingming,h.name as heyue,zu.heyue_cs,oj.baodan as baodanbh,oj.bianhao,oj.lb,bz.miaoshu as zhuangtai,oj.kp_bz,oj.num,oj.cj_num,oj.tuo_price as baodan,oj.cj_jianping_yongjin as yongjin,oj.hb_id,oj.cj_shijian,kl.name as weituoleixing,oj.tuo_shijian,oj.zhanghao_id,oj.jian_price,oj.cj_price')
//            ->order('oj.id desc,oj.tuo_shijian desc')
//            ->select();
//        $list = array_merge_recursive($list,$lis,$lis1);

        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if ($v['kp_bz'] == '1') {
                    $list[$k]['kp_bz'] = '开仓';
                } else {
                    $list[$k]['kp_bz'] = '平仓';
                }
                if ($v['zhanghao_id'] > 0) {
                    $list[$k]['zhanghao_id'] = '弘苏组';
                } else {
                    $list[$k]['zhanghao_id'] = '盈透组';
                }
                switch ($v['hb_id']) {
                    case 1:
                        $list[$k]['hb_id'] = '人民币';
                        break;
                    case 2:
                        $list[$k]['hb_id'] = '港元';
                        break;
                    case 3:
                        $list[$k]['hb_id'] = '美元';
                        break;
                    case 4:
                        $list[$k]['hb_id'] = '欧元';
                        break;
                    default:
                        $list[$k]['hb_id'] = '未知货币';
                }
                if ($v['baodan'] == 0) {
                    $list[$k]['baodan'] = '市价成交';
                }
                $list[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                if ($v['lb'] == '1') {
                    $list[$k]['pc_yk'] = (($v['jian_price'] - $v['cj_price']) * $v['heyue_cs']) * $v['cj_num'];
                } else {
                    $list[$k]['pc_yk'] = (($v['cj_price'] - $v['jian_price']) * $v['heyue_cs']) * $v['cj_num'];
                }
                //四舍五入
                if (config('web_name') == 'HC') {
                    $list[$k]['pc_yk'] = round($list[$k]['pc_yk']);
                } else {
                    $list[$k]['pc_yk'] = round($list[$k]['pc_yk'], 2);
                }
                if ($v['lb'] == '1') {
                    $list[$k]['lb'] = '买';
                } else {
                    $list[$k]['lb'] = '卖';
                }
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $list[$k]['bh'])){
                            $list[$k]['bh'] = substr_replace( $list[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
        }
        if (!empty($list)) {
            returnSuccess($list);
        } else {
            returnError('暂无数据');
        }
    }

    //跟单报表
    public function gdbb()
    {
        $data = input();
        if ($this->isCZY) {
            $user_id = $this->par_id;
        } else {
            $user_id = $this->id;
        }
        $list = db('gendan')
            ->alias('g')
            ->join('yonghu', 'yonghu.id=g.yh_id', 'LEFT')
            ->join('heyue', 'heyue.id=g.heyue_id', 'LEFT')
            ->join('yonghu y', 'y.id=g.yh_id1', 'LEFT')
            ->where('gendan.bz', 1)
            ->where('yonghu.sj_xulie', 'like', "%," . $user_id . ",%")
            ->field('gendan.num,gendan.num_lb,gendan.kaiping,gendan.gendan_lb,gendan.bz,yonghu.bh,yonghu.xingming,yonghu.sj_xulie,heyue.name,y.xingming AS gendanxingming')
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if ($v['num_lb'] == 0) {
                    $list[$k]['num_lb'] = '倍数';
                } else {
                    $list[$k]['num_lb'] = '手数';
                }
                if (empty($v['name'])) {
                    $list[$k]['name'] = '全部';
                }
                if ($v['kaiping'] == 0) {
                    $list[$k]['kaiping'] = '全部操作';
                } elseif ($v['kaiping'] == 15) {
                    $list[$k]['kaiping'] = '买入';
                } elseif ($v['kaiping'] == 17) {
                    $list[$k]['kaiping'] = '卖出';
                }
                if ($v['gendan_lb'] == 0) {
                    $list[$k]['gendan_lb'] = '正向跟单';
                } else {
                    $list[$k]['gendan_lb'] = '反向跟单';
                }
                if ($v['bz'] == 1) {
                    $list[$k]['bz'] = '正在跟单';
                }
                $list[$k]['sj_xulie'] = $this->get_dl_name($v['sj_xulie']);
                if(config('web_name') == 'HC'){
                    if(!$this->is_Dingji){
                        if(preg_match("/^1[345678]{1}\d{9}$/", $list[$k]['bh'])){
                            $list[$k]['bh'] = substr_replace( $list[$k]['bh'],'****',3,4);
                        }
                    }
                }
            }
            if (!empty($list)) {
                returnSuccess($list);
            } else {
                returnError('暂无数据');
            }
        } else {
            returnError('暂无数据');
        }

    }


}